/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujiyuu75.sequent.sample;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;

import java.util.ArrayList;

/**
 * Sequent和PageSlider配合使用的演示页面
 */
public class SampleAbility extends Ability {

    ArrayList<Component> pageview;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_sample);
        pageview = new ArrayList<>();

        pageview.add(new SampleStackLayout1(this));
        pageview.add(new SampleStackLayout2(this));
        pageview.add(new SampleStackLayout3(this));

        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager);
        DotIndicatorController dic = (DotIndicatorController) findComponentById(ResourceTable.Id_dic);
        dic.initialize(pageview.size());

        // 绑定适配器
        pageSlider.setProvider(new SamplePageSliderProvider(pageview));
        pageSlider.setCurrentPage(0);
        ((OnShowListener) pageview.get(0)).onShow();
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
            }

            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int itemPos) {
                dic.selectPosition(itemPos);
                if (pageview.get(itemPos) instanceof OnShowListener) {
                    ((OnShowListener) pageview.get(itemPos)).onShow();
                }
            }
        });

    }

}
